package com.inhim.pjhim.adapter;

import android.app.Activity;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.ReaderTypeList;
import com.inhim.pjhim.utils.ImageLoaderUtils;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

public class TypeListAdapter extends RecyclerArrayAdapter<ReaderTypeList.List> {

    private ReaderTypeList.List readerTypeList;
    private ReaderTypeList.List ReaderType;
    private Activity context;
    private String mType;
    public TypeListAdapter(Activity context,ReaderTypeList.List ReaderType,String type) {
        super(context);
        this.context=context;
        this.ReaderType=ReaderType;
        this.mType=type;
    }
    public void setReaderType(ReaderTypeList.List ReaderType){
        this.ReaderType=ReaderType;
    }
    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        PersonViewHolder personViewHolder =new PersonViewHolder(parent,viewType);
        if(viewType==0&&readerTypeList!=null){
            return personViewHolder;
        }
        return personViewHolder;
    }
    public class PersonViewHolder extends BaseViewHolder<ReaderTypeList.List> {

        ImageView iv_title;
        TextView tv_title,tv_content,tv_author,tv_section;
        LinearLayout lin_1;
        int position;
        PersonViewHolder(ViewGroup parent,int position){
            super(parent, R.layout.item_type_list);
            iv_title = getView(R.id.iv_title);
            tv_title = getView(R.id.tv_title);
            tv_content=getView(R.id.tv_content);
            tv_author=getView(R.id.tv_author);
            tv_section=getView(R.id.tv_section);
            lin_1=getView(R.id.lin_1);
        }
        @Override
        public void setData(ReaderTypeList.List data) {
            super.setData(data);
            if (position == getItemCount() - 1) {
                lin_1.setVisibility(View.GONE);
            } else {
                lin_1.setVisibility(View.VISIBLE);
            }
            try{
                /*tv_title.setTextColor(Color.parseColor("#333333"));
                tv_content.setTextColor(Color.parseColor("#333333"));
                tv_author.setTextColor(Color.parseColor("#333333"));
                tv_section.setTextColor(Color.parseColor("#333333"));*/
                setText(tv_section,"共"+data.getTotal()+"课");
                if(data.getTagName()!=null && !"".equals(data.getTagName())){
                    tv_content.setVisibility(View.VISIBLE);
                    setText(tv_content,data.getTagName());
                }else{
                    tv_content.setVisibility(View.GONE);
                }
                ConstraintLayout.LayoutParams Params   =  (ConstraintLayout.LayoutParams)iv_title.getLayoutParams();
                if(mType.equals("2")||mType.equals("1")){
                    Params.height = context.getResources().getDimensionPixelSize(R.dimen.base_dimen_85);
                    Params.width = context.getResources().getDimensionPixelSize(R.dimen.base_dimen_140);
                    setText(tv_author,data.getReadAmount()+"人已看");
                }else{
                    Params.height = context.getResources().getDimensionPixelSize(R.dimen.base_dimen_100);
                    Params.width = context.getResources().getDimensionPixelSize(R.dimen.base_dimen_80);
                    setText(tv_author,data.getReadAmount()+"人已听");
                    }
                iv_title.setLayoutParams(Params);
                setText(tv_title,data.getName());
                ImageLoaderUtils.setImage(data.getIcon(), iv_title);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    private void setText(TextView textView,String content){
        if(content!=null&&textView!=null){
            textView.setText(content);
        }
    }
}
